<template>
  <div class="list">
    <PageHeader>
      <div slot="left">
        <!-- <a-input-group compact>
          <a-select v-model="keyType">
            <a-select-option value="phone">搜手机号</a-select-option>
            <a-select-option value="name">搜用户名</a-select-option>
          </a-select>
          <a-input-search v-model="key" @input="onSearch" allowClear placeholder="搜索" style="width: 200px" />
        </a-input-group> -->
      </div>
      <div slot="right">

      </div>
    </PageHeader>

    <div class="content-head" style="margin-bottom: 20px;">
      <div class="sort-keys">
        <a-input placeholder="输入用户手机号" style="width: 180px;margin-right:10px;" v-model="key" allowClear :size="size"
          @input="onSearch" />

        <!-- <a-button :size="size" type="primary" @click="refreshData" style="margin-right:10px;">刷新</a-button>

        <span style="margin-left: 20px; margin-right: 10px;">排序</span>
        <a-select style="width: 200px;margin-right:10px;" v-model="sortBy" placeholder="排序" allowClear>
          <a-select-option :value="item.value" v-for="item in sortByList" :key="item.value">{{ item.title }}</a-select-option>
        </a-select>
           <a-select style="width: 150px;margin-right:10px;" v-model="category_id" placeholder="分类" allowClear>
          <a-select-option :value="item.id" v-for="item in category" :key="item.id">{{ item.title }}</a-select-option>
        </a-select> -->

        <span style="margin-left: 20px; margin-right: 6px;">日期:</span>
        <iDatePicker :showTime="false" style="margin-left: 10px;" placeholder="不选则不限制" v-model="filter.min_day"></iDatePicker>
        至
        <iDatePicker :showTime="false" style="margin-left: 0px;" placeholder="不选则不限制" v-model="filter.max_day"></iDatePicker>


        <span style="margin-left: 20px; margin-right: 10px;">排序</span>
        <a-select style="width: 200px;margin-right:10px;" v-model="sortBy" placeholder="排序" allowClear>
          <a-select-option :value="item.value" v-for="item in sortByList" :key="item.value">{{ item.title }}</a-select-option>
        </a-select>

      </div>
      <!-- <a-button type="primary" icon="plus" @click="handleCreate" v-if="!selectMode">创建{{resourceTypeText}}</a-button> -->
    </div>
    <a-table class="table" :rowKey="row => row.uuid" :dataSource="list" :pagination="pagination" :size="size">

      <a-table-column title="用户" width="120px">
        <template slot-scope="row">
          <div class="user" v-if="row.user">
            <img :src="row.user.headimg" alt="用户头像">
            <div>
              <div>{{ row.user.name }}</div>
              <div>{{row.user.phone}}</div>
            </div>
            <!-- <a-icon class="gender-man" v-if="row.gender==='男'" type="man" />
            <a-icon class="gender-woman" v-else type="woman" /> -->
          </div>
           <!-- <UserDisplay :info="row.user"></UserDisplay> -->
        </template>
      </a-table-column>

      <a-table-column title="总支付" width="100px">
        <template slot-scope="row">
          <NumberGroupDisplay :info="row" prefix="all_pay_"></NumberGroupDisplay>
        </template>
      </a-table-column>

      <a-table-column title="总获得" width="100px">
        <template slot-scope="row">
          <NumberGroupDisplay :info="row" prefix="all_get_"></NumberGroupDisplay>
        </template>
      </a-table-column>

      <a-table-column title="一番赏支付" width="100px">
        <template slot-scope="row">
          <NumberGroupDisplay :info="row" prefix="yfs_pay_"></NumberGroupDisplay>
        </template>
      </a-table-column>

      <a-table-column title="一番赏获得" width="100px">
        <template slot-scope="row">
          <NumberGroupDisplay :info="row" prefix="yfs_get_"></NumberGroupDisplay>
        </template>
      </a-table-column>

      <a-table-column title="福袋支付" width="100px">
        <template slot-scope="row">
          <NumberGroupDisplay :info="row" prefix="fudai_pay_"></NumberGroupDisplay>
        </template>
      </a-table-column>

      <a-table-column title="福袋获得" width="100px">
        <template slot-scope="row">
          <NumberGroupDisplay :info="row" prefix="fudai_get_"></NumberGroupDisplay>
        </template>
      </a-table-column>
      
      <a-table-column title="卡牌社支付" width="100px">
        <template slot-scope="row">
          <NumberGroupDisplay :info="row" prefix="kapaishe_pay_"></NumberGroupDisplay>
        </template>
      </a-table-column>
      
      <a-table-column title="卡牌社获得" width="100px">
        <template slot-scope="row">
          <NumberGroupDisplay :info="row" prefix="kapaishe_get_"></NumberGroupDisplay>
        </template>
      </a-table-column>

      <a-table-column title="盲盒支付" width="100px">
        <template slot-scope="row">
          <NumberGroupDisplay :info="row" prefix="box_pay_"></NumberGroupDisplay>
        </template>
      </a-table-column>

      <a-table-column title="盲盒获得" width="100px">
        <template slot-scope="row">
          <NumberGroupDisplay :info="row" prefix="box_get_"></NumberGroupDisplay>
        </template>
      </a-table-column>

      <!-- <a-table-column title="操作" width="100px" v-if="!selectMode">
        <template slot-scope="row">
          <span>
            <a href="javascript:;" @click="handleEdit(row)">编辑</a>
            <a-divider type="vertical" />
            <a href="javascript:;" @click="handleDelete(row)">删除</a>
          </span>
        </template>
      </a-table-column> -->
    </a-table>

  </div>
</template>
<script>
  import moment from 'moment'
  import ISwitch from '@/components/ISwitch/ISwitch'

  import NumberGroupDisplay from './components/NumberGroupDisplay'

  import IDatePicker from "@/components/IDatePicker/IDatePicker.vue"

  const resourceType = 'stats/user-stats'
  const resourceTypeText = '消费统计'

  export default {
    components: {
      ISwitch,
      NumberGroupDisplay,
      IDatePicker
    },
    props: {
      selectMode: {
        type: Boolean
      },
      params: {
        type: Object
      },
      size: {
        type: String,
        default: 'default'
      }
    },
    data() {
      return {
        isEdit: false,
        editUuid: '',
        resourceTypeText,
        exportOptions: {
          status: 'all',
          startTime: '',
          endTime: '',
          type: 'normal'
        },
        list: [],
        page: 1,
        per_page: 10,
        total: 0,
        key: '',
        keyType: 'phone',
        category_id: undefined,
        sortBy: undefined,
        sortByList: [
          {
            title: '总支付人民币降序',
            value: 'all_pay_money_price-desc',
          },
          {
            title: '总支付积分降序',
            value: 'all_pay_score_price-desc',
          },
          {
            title: '一番赏支付人民币降序',
            value: 'yfs_pay_money_price-desc',
          },
          {
            title: '一番赏支付积分降序',
            value: 'yfs_pay_score_price-desc',
          },
          {
            title: '福袋支付人民币降序',
            value: 'fudai_pay_money_price-desc',
          },
          {
            title: '福袋支付积分降序',
            value: 'fudai_pay_score_price-desc',
          },
          {
            title: '盲盒支付人民币降序',
            value: 'box_pay_money_price-desc',
          },
          {
            title: '盲盒支付积分降序',
            value: 'box_pay_score_price-desc',
          },
          {
            title: '总获得人民币降序',
            value: 'all_get_money_price-desc',
          },
          {
            title: '总获得积分降序',
            value: 'all_get_score_price-desc',
          },
          {
            title: '一番赏获得人民币降序',
            value: 'yfs_get_money_price-desc',
          },
          {
            title: '一番赏获得积分降序',
            value: 'yfs_get_score_price-desc',
          },
          {
            title: '福袋获得人民币降序',
            value: 'fudai_get_money_price-desc',
          },
          {
            title: '福袋获得积分降序',
            value: 'fudai_get_score_price-desc',
          },
          {
            title: '盲盒获得人民币降序',
            value: 'box_get_money_price-desc',
          },
          {
            title: '盲盒获得积分降序',
            value: 'box_get_score_price-desc',
          },
        ],
        filter: {
          min_day: '',
          max_day: ''
        }
      }
    },
    watch: {
      params() {
        this.refreshData()
      },
      category_id() {
        this.refreshData()
      },
      sortBy() {
        this.refreshData()
      },
      'filter.min_day'() {
        this.refreshData()
      },
      'filter.max_day'() {
        this.refreshData()
      }
    },
    computed: {
      pagination() {
        return {
          defaultPageSize: this.per_page,
          current: this.page,
          total: this.total,
          onChange: (e) => {
            this.page = e
            this.fetchData()
          }
        }
      }
    },
    filters: {},
    created() {
      this.fetchData()
      // this.getCategoryList()
    },
    methods: {
      setTop(row) {
        this.$confirm({
          title: '提示',
          content: `确认要置顶此帖子吗？`,
          onOk: () => {
            this.$http.put(`/${resourceType}/${row.uuid}`, {
              type: 'set_top'
            }).then(res => {
              this.fetchData()
            })
          }
        })
      },
      unsetTop(row) {
        this.$confirm({
          title: '提示',
          content: `确认要取消置顶此帖子吗？`,
          onOk: () => {
            this.$http.put(`/${resourceType}/${row.uuid}`, {
              type: 'unset_top'
            }).then(res => {
              this.fetchData()
            })
          }
        })
      },
      handleDelete(row) {
        this.$confirm({
          title: '提示',
          content: `确认删除吗？`,
          onOk: () => {
            this.$http.delete(`/${resourceType}/${row.uuid}`).then(res => {
              this.fetchData()
            })
          }
        })
      },
      updateItem(row, data) {
        return this.$http.patch(`/${resourceType}/${row.uuid}`, {
          type: 'update',
          attributes: {
            ...data
          }
        })
      },
      recommendStatusChange(e, row) {
        this.updateItem(row, {
          is_recommend: e
        }).then(res => {
          this.fetchData()
        })
      },
      listedStatusChange(e, row) {
        this.updateItem(row, {
          status: e
        }).then(res => {
          this.fetchData()
        })
      },
      handleEditListWeight(row) {
        this.$globalForm.show({
          title: '编辑排序',
          items: [{
            type: 'number',
            key: 'list_weight',
            title: '排序',
            default: row.list_weight,
            required: true
          }, ],
          success: (data) => {
            this.updateItem(row, {
              ...data
            }).then(res => {
              this.$message.success('保存成功');
              this.$globalForm.hide()
              this.fetchData()
            })
          }
        })
      },
      handleCreate() {
        this.editUuid = ""
        this.isEdit = true
      },
      handleEdit(row) {
        this.editUuid = row.uuid
        this.isEdit = true
      },
      refreshData() {
        this.page = 1
        // this.key = ''
        this.fetchData()
      },
      onSearch(e) {
        this.fetchData()
        // clearTimeout(this.timeout)
        // this.timeout = setTimeout(() => {
        //   this.fetchData()
        // }, 300)
      },
      fetchData() {
        this.isEdit = false
        this.$http.get(`/${resourceType}`, {
          params: {
            ...this.params,
            page: this.page,
            per_page: this.per_page,
            key: this.key,
            sort_by: this.sortBy,
            min_day: this.filter.min_day,
            max_day: this.filter.max_day
          }
        }).then(res => {
          this.list = res.data.list
          this.total = res.data.item_total
        }).finally(() => {
          this.loading = false
        })
      },
      getCategoryList() {
        this.$api.emit('category.list', 'article').then(res => {
          this.category = res.data.list
        })
      },
    }
  }
</script>
<style lang="scss" scoped>
  .top-btn {
    background: #1890FF;
    color: white;
    cursor: pointer;
    font-size: 80%;
    border-radius: 4px;
    padding: 2px 6px;

    &.unset {
      border: 1px solid #D8D5D5;
      background: #D8D5D5;
      color: gray;
    }
  }

  .content-head {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
  }

  .table {
    .thumb {
      height: 60px;
      border-radius: 6px;
    }

    .content-image {
      height: 60px;
      width: 60px;
      margin-right: 10px;
      margin-bottom: 10px;
      object-fit: cover;
      border-radius: 10px;
      cursor: pointer;

      &:hover {
        opacity: 0.8;
      }
    }

    .user {
      display: flex;
      // flex-wrap: wrap;
      align-items: center;

      img {
        width: 26px;
        height: 26px;
        margin-right: 6px;
        border-radius: 50%;
        overflow: hidden;
      }

      .gender-man,
      .gender-woman {
        margin-left: 5px;
        font-size: 12px;
      }

      .gender-man {
        color: #409EFF;
      }

      .gender-woman {
        color: #F56C6C;
      }
    }
  }
</style>
